package com.example.demo.demos.controller;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 17219
 * @version 1.0
 * @date 2024/12/6 11:35
 */
@RestController
@RequestMapping("/pdf/down")
public class DownController {

    @PostMapping
    public void down(@RequestBody byte[] bytes, HttpServletResponse response) throws Exception {

        List<String> list = new ArrayList<>();
        list.add("C:\\Users\\17219\\Desktop\\cess.png");
        list.add("C:\\Users\\17219\\Desktop\\ces2.png");
//        mergeImages(list, "C:\\Users\\17219\\Desktop\\cessNew.png");
//        createPDF("C:\\Users\\17219\\Desktop\\cessNew.png", "C:\\Users\\17219\\Desktop\\cessNew.pdf");

        File file = new File("C:\\Users\\17219\\Desktop\\ces.txt");

        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] bytes = decoder.decodeBuffer(str);

//        BufferedReader reader = new BufferedReader(new FileReader(file));
//        String line;
//        StrBuilder sb = new StrBuilder();
//        while ((line = reader.readLine()) != null) {
//            sb.append(line);
//        }
//        str = sb.toString();

        //base64 转 pdf
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\17219\\Desktop\\out.pdf");
        fileOutputStream.write(bytes);
        fileOutputStream.close();

        //html 转 pdf
//        ITextRenderer iTextRenderer = new ITextRenderer();
//        iTextRenderer.setDocument(str);
//        iTextRenderer.layout();
//        iTextRenderer.createPDF(Files.newOutputStream(Paths.get("")));
    }
/*


    public static void main(String[] args) throws Exception {
//
//        List<String> list = new ArrayList<>();
//        list.add("C:\\Users\\17219\\Desktop\\cess.png");
//        list.add("C:\\Users\\17219\\Desktop\\ces2.png");
//        mergeImages(list, "C:\\Users\\17219\\Desktop\\cessNew.png");
//        createPDF("C:\\Users\\17219\\Desktop\\cessNew.png", "C:\\Users\\17219\\Desktop\\cessNew.pdf");

        File file = new File("C:\\Users\\17219\\Desktop\\ces.txt");

        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }


        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        //加载html模版
        imageGenerator.loadHtml(sb.toString());
        //把html写入到图片
        imageGenerator.saveAsImage("C:\\Users\\17219\\Desktop\\cesPng.png");
        BufferedImage bufferedImage = imageGenerator.getBufferedImage();
        mergeImages(Lists.newArrayList(bufferedImage), "C:\\Users\\17219\\Desktop\\cessNew.png");
        createPDF("C:\\Users\\17219\\Desktop\\cessNew.png", "C:\\Users\\17219\\Desktop\\cessNew.pdf");
    }

    */
/**
     * 将多个图片文件合并成一张长图
     *
     * @param imageFiles      图片文件列表
     * @param mergedImageFile 合并后的长图文件
     * @throws Exception
     *//*

    public static void mergeImages(List<BufferedImage> imageFiles, String mergedImageFile) throws Exception {
        BufferedImage mergedImage = null;
        int heightTotal = 0;
        int maxWidth = 0;
        for (BufferedImage image : imageFiles) {
            //图片大小不保持一致需要把这个判断注释掉，不然会出现黑框
//            if (image.getWidth() > maxWidth) {
            maxWidth = image.getWidth();
//            }
            heightTotal += image.getHeight();
        }
        mergedImage = new BufferedImage(maxWidth, heightTotal, BufferedImage.TYPE_INT_RGB);
        int offsetY = 0;
        for (BufferedImage image : imageFiles) {
            mergedImage.getGraphics().drawImage(image, 0, offsetY, null);
            offsetY += image.getHeight();
        }
        ImageIO.write(mergedImage, "jpg", new File(mergedImageFile));
    }

    */
/**
     * 将长图文件转换为PDF文件
     *
     * @param mergedImageFile 长图文件
     * @param pdfFile         PDF文件
     * @throws Exception
     *//*

    public static void createPDF(String mergedImageFile, String pdfFile) throws Exception {
        //获取下图片的大小，图片的大小就是document的大小（建议图片就是A4纸的大小就行）
        Image image = Image.getInstance(mergedImageFile);
        com.itextpdf.text.Rectangle rectangle = new Rectangle(image.getWidth(), image.getHeight());

        Document document = new Document(rectangle, 0, 0, 0, 0);
        // 创建PdfWriter对象
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
        // 打开文档
        document.open();
        // 获取PdfContentByte对象
        PdfContentByte cb = writer.getDirectContent();
        image.scaleToFit(document.getPageSize().getWidth(), document.getPageSize().getHeight());
        // 设置图片位置
        image.setAbsolutePosition(0, 0);
        // 添加图片到PdfContentByte对象
        cb.addImage(image);
        // 关闭文档
        document.close();
    }

*/

}
